package tr.com.netgis;

import tr.com.netgis.util.Constants;
import tr.com.netgis.util.EulaUtils;
import tr.com.netgis.util.Utility;
import tr.com.netgis.web.LoginRequest;
import tr.com.netgis.web.LoginResponse;
import tr.com.netgis.web.WebServisCaller;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Toast;

public class SplashScreen extends Activity {
	private static final int DIALOG_EULA_ID = 0;

	protected boolean _active = true;
	protected int _splashTime = 3000;

	private static Object lock = new Object();
	private static boolean running = false;

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		dismissProgress();
		synchronized (lock) {
		}
		super.onConfigurationChanged(newConfig);
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash);

	}

	class LoginTask extends AsyncTask<Void, Void, LoginResponse> {

		protected LoginResponse doInBackground(Void... vs) {
			return postData();
		}

		protected void onProgressUpdate(Void... progress) {
		}
		
		@Override
		protected void onPreExecute() {
			running = true;
			super.onPreExecute();
		}

		protected void onPostExecute(LoginResponse response) {
			running = false;
			SplashScreen.this.finish();
			if (response != null) {
				if (response.hasSession()) {
					startActivity(new Intent("tr.com.netgis.MainActivity"));
				} else {
					String failMsg = getResources().getString(
							R.string.msgLoginFailure);
					Toast.makeText(SplashScreen.this, failMsg,
							Toast.LENGTH_SHORT).show();
					startActivity(new Intent(SplashScreen.this,
							LoginActivity.class));
				}
			}
			dismissProgress();
		}
	};

	private void startLogin() {	
		dialog = ProgressDialog.show(SplashScreen.this, "", getWaitMessage(), true);
		new LoginTask().execute();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		dismissProgress();
	}
	
	@Override
	protected void onPostResume() {
		super.onPostResume();
		if (!running)
			startLogin();
	}
	
	@Override
	protected void onResume() {
		if (running) {
			if (dialog != null) {
				dialog.show();
			} else {
				dismissProgress();
				dialog = ProgressDialog.show(this, "", getWaitMessage(), true);				
			}
		}		
		super.onResume();
	}

	private String getWaitMessage () {
		String waitMsg = getResources().getString(R.string.msgLoginWait);
		return waitMsg;
	}
	
	private LoginResponse postData() {
		SharedPrefs prefs = SharedPrefs.getInstance();
		String password = prefs.getPassword();
		String userName = prefs.getUserName();
		if (Utility.isEmpty(userName)) {
			startActivity(new Intent(SplashScreen.this, LoginActivity.class));
			return null;
		}

		LoginRequest loginRequest = new LoginRequest();
		loginRequest.setPassword(password);
		loginRequest.setUserName(userName);
		loginRequest.setShowFailureNotification(false);
		loginRequest.setServerUrl(Constants.SERVER_URL);
		return WebServisCaller.callWebService(loginRequest);
	}

	static ProgressDialog dialog;

	private void dismissProgress() {
		dialog.dismiss();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			// _active = false;
		}
		return true;
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		return new AlertDialog.Builder(this)
				.setCancelable(true)
				.setMessage(EulaUtils.getEulaMessage(this))
				.setNegativeButton(R.string.decline,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								finish();
							}
						})
				.setOnCancelListener(new DialogInterface.OnCancelListener() {
					@Override
					public void onCancel(DialogInterface dialog) {
						finish();
					}
				})
				.setPositiveButton(R.string.accept,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
				                EulaUtils.setEulaValue(SplashScreen.this);
								startLogin();
							}
						}).setTitle(R.string.eula_title).create();
	}

}